<?php
class helper_advertise extends system_view_helper_abstract {
	
	protected $_name = null;
	protected $_data = array();
	protected $_lang = 'en_us';
	protected $_time = null;
	
	public function __construct() {
		$this->_lang = system_nav_language::getInstance()->getName();
		$this->_time = system_date::timestamp();
	}
	/**
	 * @return helper_advertise
	 */
	public function advertise($name = null) {
		$this->_name = $name;
		return $this;
	}

	protected function getAd($device) {
		$ad = $this->getAdvertise();
		if(empty($ad)) return null;
		if(empty($ad['items'])) {
			return $this->defaultAd($ad)->add();
		}
		$item_key = $this->findItem($ad, $device);
		if(!isset($ad['items'][$item_key])) {
			return $this->defaultAd($ad)->add();
		}
		$item = $ad['items'][$item_key];
		$_ad = $this->defaultAd($ad);
		return $_ad->setScript($item['script'])
							->setStyle($item['style'])
							->setScriptBody($item['script_body'])
							->setStyleBody($item['style_body'])
							->setIsLayout($item['is_layout'])->add();
		return null;
	}
	
	public function remove() {
		return @unlink($this->getCache()->_idToFileName($this->_name));
	}
	
	protected function findItem($ad, $device) {
		foreach($ad['items'] AS $key => $item) {
			if(empty($item['devices']) || empty($item['langs'])) break;
			foreach($item['devices'] AS $dev) {
				if(strtolower($dev['device_key']) == strtolower($device)) {
					foreach($item['langs'] AS $la) {
						if(strtolower($la['lang']) == strtolower($this->_lang) && 
								$la['start_time'] <= $this->_time && $la['end_time'] >= $this->_time) {
							return $key;
						}
					}
				}
			}
		}
		return 0;
	}
	
	/**
	 * @return system_nav_advertise
	 */
	protected function defaultAd($ad) {
		$url = $ad['link'] ? $this->view->url('index/link', array('key' => $ad['link'])) : $this->view->url('index/index');
		$_ad = new system_nav_advertise($this->_name);
		$_ad->setHeight($ad['height'])->setWidth($ad['width'])->setResource($ad['resource'])
			->setUrl((string) $url);
		return $_ad;
	}
	
	public function render($device = 'web', $script = 'include/advertise.phtml') {
		if(($ad = $this->getAd($device)) === null) {
			$ad = new system_nav_advertise($this->_name);
			$ad = $ad->add();
		}
		$this->view->ad = $ad;
		return $this->view->render($script);
	}
	
	public function __toString() {
		return $this->render();
	}
	
	protected $_cache = null;
	/**
	 * @return system_cache_file
	 */
	protected function getCache() {
		if($this->_cache === null) {
			$this->_cache = system_cache::factory('advertise');
		}
		return $this->_cache;
	}

	protected function getAdvertise() {
		$name = $this->_name;	
		if(!isset($this->_data[$name])) {
			$cache = $this->getCache();
			if(!($this->_data[$name] = $cache->load($name))) {
				$result = $this->_advertise($name);
				$cache->save($name, $result);
				$this->_data[$name] = $result;
			}
		}
		return $this->_data[$name];
	}

	protected function _advertise($name) {
		$concept = new resource_advertise_concept();
		if(!($row = $concept->fetch(array('name = ?' => $name)))) {
			return array();
		}
		$ad = $row->toArray();
		$ad['resource'] = $row->getResource()->getOriginal();
		$ad['items'] = array();
		$concept = new resource_advertise_item_concept();
		$dConcept = new resource_advertise_device_concept();
		$lConcept = new resource_advertise_lang_concept();
		foreach($concept->fetchAll(array('item.advertise_id = ?' => $row->getId()), array('item.display_order ASC', 'item.id DESC')) AS $key => $item) {
			$ad['items'][$key] = $item->toArray();
			$ad['items'][$key]['resource'] = $item->getResource()->getOriginal();
			$ad['items'][$key]['langs'] = $lConcept->fetchAll(array('item_id = ?' => $item->getId()))->toArray();
			$ad['items'][$key]['devices'] = $dConcept->fetchAll(array('item_id = ?' => $item->getId()))->toArray();
		}
		
		return $ad;
	}
	
}
